package com.shuizhu.template.demo4;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.metadata.property.FontProperty;
import com.alibaba.excel.metadata.property.StyleProperty;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AccountingFill {
  
  public static void main(String[] args) throws ParseException {
    //要填充的Excel模板的路径
    String templatePath = "C:\\Users\\tianyunjie\\Desktop\\temp\\response.xlsx";
    //填充后文件的保存路径
    String savePath = "C:\\Users\\tianyunjie\\Desktop\\temp\\response-demo.xlsx";

    //工作簿对象
    ExcelWriter workBook = EasyExcel.write(savePath, Bill.class)
        .withTemplate(templatePath)
        .build();

    //工作表对象
    WriteSheet sheet = EasyExcel.writerSheet().build();

    //配置多组数据填充完后，需要换行，防止覆盖模板中的单组数据模板
    FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();

    //获取要填充的数据
    List<Bill> bills = getBills();

    //先填充出来多组数据（账单详情数据）
    workBook.fill(bills, fillConfig, sheet);

    //填充单组数据（汇总数据）
    long totalCount = bills.size();//账单条数
    double totalAmount = bills.stream().mapToDouble(Bill::getAmount).sum();//账单总金额

    HashMap<String, String> summaryMap = new HashMap<>();
    summaryMap.put("totalCount","fadfa".trim());
    summaryMap.put("totalAmount","23423423rfaf".trim());

   // FillConfig fillConfig2 = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
    //手动单独填充单组数据
    workBook.fill(summaryMap, sheet);


    //关闭流
    workBook.finish();
  }
  
  /**
   * 生成账单数据
   */
  private static List<Bill> getBills() throws ParseException {
    List<Bill> list = new ArrayList<>();
    
    String datas = "2022-10-11\t支出\t购物\t18.36\t这里是备注内容\n" +
        "2022-10-11\t支出\t餐饮\t6\t这里是备注内容\n" +
        "2022-10-11\t支出\t学习\t1\t这里是备注内容\n" +
        "2022-10-11\t支出\t餐饮\t9\t这里是备注内容\n" +
        "2022-10-11\t支出\t学习\t1.5\t这里是备注内容\n" +
        "2022-10-10\t支出\t洗澡\t0.6\t这里是备注内容\n" +
        "2022-10-10\t支出\t餐饮\t6\t这里是备注内容\n" +
        "2022-10-10\t支出\t网络支出\t1\t这里是备注内容\n" +
        "2022-10-10\t支出\t餐饮\t9\t这里是备注内容";
    
    //分解出每一条数据
    for (String bill : datas.split("\n")) {
      String[] bills = bill.split("\t");
      Bill bill1 = new Bill(new SimpleDateFormat("yyyy-MM-dd").parse(bills[0]),
                  bills[1],
                  bills[2],
                  Double.parseDouble(bills[3]),
                  bills[4]);
      list.add(bill1);
    }
    return list;
  }
  
}

